<template>
  <div class="clas_All">
    <el-form class="const_top" :model="queryParams" size="small" :inline="true">
      <el-row>
        <el-col :span="24">
          <el-form-item prop="" style="height: 40px">
            <el-select
              v-model="byMonth.relateId"
              placeholder="请选择部门"
              clearable
              @change="deptChange"
            >
              <el-option
                v-for="dict in deptOptions"
                :key="dict.relateId"
                :label="dict.label"
                :value="dict.relateId"
              />
            </el-select>
          </el-form-item>
          <el-form-item prop="" style="height: 40px">
            <el-date-picker
              clearable
              v-model="queryParams.createdTime"
              type="month"
              placeholder="选择月"
              @change="createdTimeChange"
              :disabled="mouday"
            >
            </el-date-picker>
          </el-form-item>
          <el-form-item prop="" style="height: 40px">
            <el-select
              v-model="queryParams.pandianCode"
              placeholder="1号至10号"
              clearable
              @change="pandianCodeChange"
              :disabled="mount"
            >
              <el-option
                v-for="dict in NewDropDownList"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <!-- <el-button type="primary" size="mini" @click="handleQuery"
              >查询
            </el-button> -->
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-row>
      <el-col class="clas_Box">
        <el-button
          size="medium"
          type="primary"
          @click="OpenSwitch(0)"
          :disabled="ShowBom"
        >
          无成本卡产品
        </el-button>
        <el-button
          size="medium"
          type="primary"
          @click="OpenSwitch(1)"
          :disabled="ShowBom"
        >
          成本卡偏小产品
        </el-button>
        <el-button
          size="medium"
          type="primary"
          :disabled="ShowBom"
          @click="OpenSwitch(2)"
        >
          成本卡偏大产品
        </el-button>
        <el-button
          :disabled="ShowBom"
          type="primary"
          size="medium"
          @click="OpenSwitch(3)"
        >
          成本卡未使用原料
        </el-button>
        <el-button
          :disabled="ShowBom"
          type="primary"
          size="medium"
          @click="OpenSwitch(4)"
        >
          无审定成本卡产品
        </el-button>
        <el-button
          :disabled="ShowBom"
          type="primary"
          size="medium"
          @click="OpenSwitch(5)"
        >
          两期盘盈
        </el-button>
        <el-button
          :disabled="ShowBom"
          type="primary"
          size="medium"
          @click="OpenSwitch(6)"
        >
          两期盘亏
        </el-button>
      </el-col>
    </el-row>
    <el-row>
      <el-col>
        <eltab
          v-if="fack == 0"
          :columns="columns"
          :strFack="fack"
          :table-data="tableData"
          @row-click="handleRowClick"
        ></eltab>
        <eltab
          v-if="fack == 1"
          :columns="column1"
          :strFack="fack"
          :table-data="tableData"
          @row-click="handleRowClick"
        ></eltab>
        <eltab
          v-if="fack == 2"
          :columns="column2"
          :strFack="fack"
          :table-data="tableData"
          @row-click="handleRowClick"
        ></eltab>
        <eltab
          v-if="fack == 3"
          :columns="column4"
          :strFack="fack"
          :table-data="tableData"
          @row-click="handleRowClick"
        ></eltab>
        <eltab
          v-if="fack == 4"
          :columns="column3"
          :strFack="fack"
          :table-data="tableData"
          @row-click="handleRowClick"
        ></eltab>
        <eltab
          v-if="fack == 5"
          :columns="column5"
          :strFack="fack"
          :showcompore="true"
          :table-data="tableData"
          @row-click="handleRowClick"
        ></eltab>
        <eltab
          v-if="fack == 6"
          :columns="column5"
          :showcompore="true"
          :strFack="fack"
          :table-data="tableData"
          @row-click="handleRowClick"
        ></eltab>
      </el-col>
    </el-row>
    <el-dialog
      title="原料明细"
      :visible.sync="dialogVisible"
      width="70%"
      :before-close="handleClose"
    >
      <el-table :data="strfack67" style="width: 100%">
        <el-table-column prop="stapName" label="物料名称" align="center">
        </el-table-column>
        <el-table-column align="center" label="损益金额">
          <el-table-column
            align="center"
            prop="sumLastProlossAmout"
            label="上期"
          >
          </el-table-column>
          <el-table-column
            align="center"
            prop="sumThisProlossAmout"
            label="本期"
          >
          </el-table-column>
          <el-table-column align="center" prop="prolossAmoutDif" label="差">
          </el-table-column>
        </el-table-column>
        <el-table-column align="center" label="操作">
          <template slot-scope="scope">
            <div>
              <el-button type="text" size="mini" @click="Showxq(scope.row)">
                查看详情
              </el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogVisible = false">
          确 定
        </el-button>
      </span>
    </el-dialog>
    <el-dialog
      title="该物料信息核算表"
      :visible.sync="StapdialogVisible"
      width="30%"
      :before-close="handleClose"
    >
      <div style="margin: 8px">
        <el-table :data="tableDataOne" style="width: 100%">
          <el-table-column>
            <el-table-column prop="data" align="center" label="用途">
            </el-table-column>
            <el-table-column align="center" label="库存单位（单位）">
              <el-table-column prop="num" align="center" label="数量">
              </el-table-column>
              <el-table-column prop="price" align="center" label="均价">
              </el-table-column>
            </el-table-column>
            <el-table-column prop="amout" align="center" label="金额">
            </el-table-column>
            <el-table-column align="center" label="操作">
              <template slot-scope="scope">
                <div v-if="scope.row.show">
                  <el-button size="mini" type="text" @click="GoOpen(scope.row)">
                    {{ scope.row.data }}
                  </el-button>
                </div>
              </template>
            </el-table-column>
          </el-table-column>
        </el-table>
        <el-button @click="StapdialogVisible = false">关闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import eltab from "@/components/ElTable/index";
import {
  unUsedBom,
  stapByunUsedBom,
  statFoodUnlocked,
  bomCardParse,
  twoPeriodCompare,
  twoPeriodCompareDetail,
  originalStapProlossStapDetail,
} from "@/api/system/cost";
import { getPanDianCode, FindShopBom } from "@/api/system/stock";
import e from "cors";
export default {
  components: { eltab },
  name: "profit_and_loss",
  data() {
    return {
      queryParams: {},
      tableData: [],
      fack: 0,
      mouday: true,
      mount: true,
      ShowBom: true,
      dialogVisible: false,
      StapdialogVisible: false,
      byMonth: {},
      columns: [
        { label: "盘点编号", prop: "pandianCode" },
        { label: "产品编号", prop: "foodCode" },
        { label: "名称", prop: "foodName" },
        { label: "单位", prop: "unit" },
        { label: "销售份数", prop: "sumcount" },
        { label: "销售金额", prop: "sumAmount" },
      ],
      column1: [
        { label: "部门" },
        { label: "产品编号" },
        { label: "产品名称" },
        { label: "单位" },
        { label: "规格种类" },
        { label: "设置单价" },
        { label: "折前毛利率(库)" },
        { label: "折前毛利率(用)" },
        { label: "折前销售" },
        { label: "销售份数" },
      ],
      column2: [
        { label: "部门" },
        { label: "产品编号" },
        { label: "产品名称" },
        { label: "单位" },
        { label: "规格种类" },
        { label: "设置单价" },
        { label: "利率(库)" },
        { label: "利率(用)" },
        { label: "折前销售" },
        { label: "销售份数" },
      ],
      deptOptions: [],
      column3: [
        { label: "店铺编码", prop: "shopCode" },
        { label: "盘点编码", prop: "pandianCode" },
        { label: "菜品名称", prop: "foodName" },
        { label: "菜品编码", prop: "foodCode" },
        { label: "份数", prop: "count" },
        { label: "价格", prop: "price" },
        { label: "销售总金额", prop: "sumAmount" },
      ],
      column4: [
        { label: "物料编码", prop: "stapCode" },
        { label: "物料名称", prop: "stapName" },
        { label: "单位", prop: "unitName" },
        { label: "价格", prop: "price" },
        { label: "数量", prop: "inNum" },
        { label: "金额", prop: "inAmout" },
      ],
      column5: [
        { label: "工作组", prop: "deptName" },
        { label: "物料种类", prop: "stapCount" },
        { label: "本期损益", prop: "sumThisProlossAmout" },
        { label: "上期损益", prop: "sumLastProlossAmout" },
      ],
      NewDropDownList: [],
      strfack67: [],
      tableDataOne: [],
    };
  },
  created() {
    FindShopBom().then((res) => {
      if (res.code == 200) {
        let ovj = res.data;
        if (ovj.length > 1) {
          this.mouday = true;
        }
        this.deptOptions = ovj;
      }
    });
  },
  methods: {
    OpenClcik() {},
    handleRowClick(v) {
      console.log(v);
      if (v.type == 5) {
        twoPeriodCompareDetail({ ids: v.ids }).then((res) => {
          if (res.code == 200) {
            this.strfack67 = res.data;
            this.dialogVisible = true;
          }
        });
      } else if (v.type == 6) {
        twoPeriodCompareDetail({ ids: v.ids }).then((res) => {
          if (res.code == 200) {
            this.strfack67 = res.data;
            this.dialogVisible = true;
          }
        });
      }
    },
    handleQuery() {
      OpenSwitch(this.fack);
    },
    OpenSwitch(v) {
      console.log(v);
      if (v == 0) {
        unUsedBom(this.queryParams).then((res) => {
          if (res.code == 200) {
            this.tableData = res.data;
          }
        });
      } else if (v == 3) {
        stapByunUsedBom().then((res) => {
          if (res.code == 200) {
            this.tableData = res.data;
          }
        });
      } else if (v == 4) {
        statFoodUnlocked().then((res) => {
          if (res.code == 200) {
            this.tableData = res.data;
          }
        });
      } else if (v == 1) {
        bomCardParse({
          type: 2,
          pandianCode: this.queryParams.pandianCode,
        }).then((res) => {
          if (res.code == 200) {
            this.tableData = res.data;
            this.$forceUpdate();
          }
        });
      } else if (v == 2) {
        bomCardParse({
          type: 1,
          pandianCode: this.queryParams.pandianCode,
        }).then((res) => {
          if (res.code == 200) {
            this.tableData = res.data;
          }
        });
      } else if (v == 5) {
        twoPeriodCompare({
          flag: 1,
          pandianCode: this.queryParams.pandianCode,
        }).then((res) => {
          if (res.code == 200) {
            this.tableData = res.data;
          }
        });
      } else if (v == 6) {
        twoPeriodCompare({
          flag: 2,
          pandianCode: this.queryParams.pandianCode,
        }).then((res) => {
          if (res.code == 200) {
            this.tableData = res.data;
          }
        });
      }
      this.fack = v;
    },

    deptChange(v) {
      // console.log(v);
      if (v) {
        this.mouday = false;
      } else {
        this.mouday = true;
      }
    },
    pandianCodeChange(v) {
      if (v != "") {
        this.ShowBom = false;
      } else {
        this.ShowBom = true;
      }
    },
    createdTimeChange(v) {
      let data = new Date(v);
      let y = data.getFullYear();
      let m = data.getMonth() + 1;
      this.byMonth.pandianData = y + String(m).padStart(2, "0");
      getPanDianCode(this.byMonth).then((res) => {
        if (res.code == 200) {
          this.DropDownList = res.data;
          this.NewDropDownList = [];
          this.DropDownList.forEach((element) => {
            let label =
              new Date(element.startTime).getDate() +
              "号 - " +
              new Date(element.endTime).getDate() +
              "号";
            this.NewDropDownList.push({
              value: element.pandianCode,
              label: label,
            });
          });
          if (res.data != []) {
            this.mount = false;
          }
        }
      });
    },
    handleNodeClick(data) {
      this.queryParams.shopCode = data.relateId;
      this.getANENd();
    },
    handleClose() {},
    Showxq(v) {
      // sku-detail
      console.log(v);
      this.$router.push({
        path: "/sku-detail",
        query: {
          pandianCode: this.queryParams.pandianCode,
          shopCode: v.shopCode,
          deptCode: v.deptCode,
          stapCode: v.stapCode,
        },
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.clas_All {
  margin: 20px;
  .clas_Box {
    margin-bottom: 10px;
  }
}
</style>
